package org.ftz;
import org.ftz.JudgeOnline.CPP.ExecuteCLangService;
import org.ftz.JudgeOnline.Java.ExecuteJava;
import org.ftz.control.*;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;
public class client extends MainFrame implements ActionListener, KeyListener, ChangeListener {
    private BufferedWriter bw=null;
    Socket s = null;
    public JPanel jPanel;
    public JButton jButton1;
    public JButton jButton2;
    public JButton jButton;
    public JLabel label;
    public JTextField jTextField;
    public int a=0;
    public DataInputStream dis;
    protected OutputStream outAudio;
    public InputStream inAudio;
    public byte[] bos = new byte[2024];
    public byte[] bis = new byte[2024];
    public TargetDataLine targetDataLine;
    public SourceDataLine sourceDataLine;
    public ClientWindow cw;
    public String input;
    Socket socket;
    Socket socketFile;
    PrintWriter printWriter;
    DataInputStream dis1=null;
    String ip = "";
    int port = 0;
    Socket socketAudio = null;
    Socket socketcb;
    String fullPath;
    public client() throws IOException {
        mainFrame();
    }
    public static void main(String[] args) throws IOException {
        try {
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            UIManager.put("RootPane.setupButtonVisible",false);
            UIManager.put("TabbedPane.tabAreaInsets",new javax.swing.plaf.InsetsUIResource(0,0,2,20));
            BeautyEyeLNFHelper.translucencyAtFrameInactive=false;
        }catch (Exception e){
            System.out.println("加载炫彩皮肤失败！");
        }
        new client();
    }
    public void mainFrame() throws IOException {
        super.mainFrame1();
        panel2.setBackground(Color.gray);
        panel2.setLayout(null);
        jPanel=new JPanel();
        jPanel.setLayout(null);
        label=new JLabel("请输入地址:端口");
        jTextField=new JTextField(10);
        label.setFont(new Font("微软雅黑",Font.BOLD,18));
        jPanel.setBounds(250,100,400,200);
        label.setBounds(130,10,200,30);
        jTextField.setBounds(100,50,200,30);
        jButton1=new JButton("确定");
        jButton2=new JButton("取消");
        jButton1.setBounds(110,90,80,30);
        jButton2.setBounds(210,90,80,30);
        label.setVisible(true);
        jPanel.add(label);
        jPanel.add(jButton1);
        jPanel.add(jButton2);
        jPanel.add(jTextField);
        jButton=new JButton("开启语音");
        jButton.setBounds(140,130,120,30);
        jButton.setVisible(false);
        jPanel.add(jButton);
        jPanel.setBackground(Color.WHITE);
        jPanel.setVisible(true);
        panel2.add(jPanel);
        super.mainFrame2();
        jButtonSelect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jFileChooser=new JFileChooser();
                jFileChooser.showOpenDialog(null);
                fullPath = jFileChooser.getSelectedFile().toString();
                String Filename = jFileChooser.getSelectedFile().getName();
                System.out.println(fullPath);
                System.out.println(Filename);
                jTextAreaFile.setText(Filename);
            }
        });
        jButtonSend.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendFile();
                System.out.println("客户端传输文件");
                jTextAreaFile.setText("");
            }
        });
        cleanBtn.addActionListener(this); //清空
        runBtn.addActionListener(this); //运行
        clearAll.addActionListener(this);
        jb.addActionListener(this); //给按钮添加点击事件
        jtf.addKeyListener(this); //给文本框添加键盘点击事件
        jButton1.addActionListener(this); //远程控制
        tabbedPane.addChangeListener(this);
        jButton.addActionListener(this);
    }
    public void control(){
        String input = jTextField.getText();
        if (null == input || "".equals(input.trim())) {
            JOptionPane.showMessageDialog(null, "地址初始失败", "地址初始失败", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String[] split = input.split(":");
        if(split.length!=2){
            JOptionPane.showMessageDialog(null, "地址初始失败", "写入格式错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            ip = split[0];
            port = Integer.parseInt(split[1]);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "地址初始失败", "地址端口初始化失败", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            System.out.println(ip + " " + port);
            s = new Socket(ip, port);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
    @Override
    public void stateChanged(ChangeEvent e) {
        String title = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
        if(title.equals("聊天")){
            JFrame jFrame=new JFrame();
            jFrame.setSize(400,300);
            jFrame.setTitle("设置");
            jFrame.setResizable(false);
            jFrame.setLocationRelativeTo(null);
            jFrame.setVisible(true);
            JPanel jPanel=new JPanel();
            jPanel.setLayout(null);
            JLabel iplabel=new JLabel("ip地址");
            iplabel.setFont(new Font("微软雅黑",Font.BOLD,18));
            iplabel.setBounds(70,20,80,25);
            jPanel.add(iplabel);
            JTextField ipjTextField=new JTextField(20);
            ipjTextField.setBounds(140,20,160,25);
            jPanel.add(ipjTextField);
            JLabel namelabel=new JLabel("昵称");
            namelabel.setFont(new Font("微软雅黑",Font.BOLD,18));
            namelabel.setBounds(85,65,80,25);
            jPanel.add(namelabel);
            JTextField namejTextField=new JTextField(20);
            namejTextField.setBounds(140,65,160,25);
            jPanel.add(namejTextField);
            JButton yes=new JButton("确定");
            yes.setBounds(100,110,60,30);
            jPanel.add(yes);
            jFrame.add(jPanel);
            yes.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String ip=ipjTextField.getText();
                    input=namejTextField.getText();
                    System.out.println(input+" "+ip);
                    jFrame.dispose();
                    try {
                        socket=new Socket(ip, 8888);
                        new Thread(new receive(socket)).start();
                    } catch (Exception ioException) {
                        ioException.printStackTrace();
                    }
                    try {
                        socketFile=new Socket(ip,3999);
                        new Thread(new receiveFile("D:\\BigProgram\\",socketFile)).start();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                    System.out.println("1"+input);
                    jf.setTitle("程序设计远程协助系统("+input+"端)");
                    try {
                        OutputStream outputStream = socket.getOutputStream();
                        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
                        printWriter=new PrintWriter(outputStreamWriter);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                    String sendstr=input.length()+input+"@";
                    System.out.println("姓名"+input);
                    printWriter.println(sendstr);
                    printWriter.flush();
                }
            });
        }
    }
    class ReceiveImage implements Runnable{
        @Override
        public void run() {
            byte[] imageBytes;
            while (true) {
                try {
                    imageBytes = new byte[dis.readInt()]; // 先拿到传过来的数组长度
                    dis.readFully(imageBytes); // 所有的数据存放到byte中
                    cw.repainImage(imageBytes);
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null, "远程失败", "远程过程失败：" + e.getMessage(), JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String code = codeArea.getText();
        String inputAreaText = inputArea.getText();
        if(e.getActionCommand().equals("清空")){
            codeArea.setText("");
            System.out.println(jComboBox.getSelectedIndex());
        }else if(e.getActionCommand().equals("点击运行")&&(jComboBox.getSelectedIndex()==0)){
            util.writeFile(inputAreaText);
            ExecuteCLangService executeCLangService = new ExecuteCLangService();
            String out = executeCLangService.runCLangCode(code);
            System.out.println(out);
            String[] split = out.split("@");
            outputArea.setText(split[0]+System.lineSeparator());
            outputArea.append(split[1]);
        }else if(e.getActionCommand().equals("点击运行")&&(jComboBox.getSelectedIndex()==1)){
            String output = ExecuteJava.runJava(code, inputAreaText);
            String[] split = output.split("@");
            outputArea.setText(split[0]+System.lineSeparator());
            outputArea.append(split[1]);
        } else if(e.getActionCommand().equals("发送")){
            senddata();
        }else if(e.getActionCommand().equals("清空所有")){
            codeArea.setText("");
            inputArea.setText("");
            outputArea.setText("");
        }else if(e.getActionCommand().equals("确定")){
            control();
            jButton.setVisible(true);
            DataOutputStream dos = null; //将客户端与服务器端链接的输出流交个ImageThread处理
            try {
                OutputStream outputStream = s.getOutputStream();
                dos = new DataOutputStream(outputStream);
                ImageThread imageThreadRun = new ImageThread(dos, 50);
                Thread imageThread = new Thread(imageThreadRun);
                imageThread.start();
                new Thread(new EventThread(s.getInputStream())).start();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
        else if(e.getActionCommand().equals("开启语音")){
            jButton.setText("关闭语音");
            try { //语音
                socketAudio = new Socket(ip, port + 1);
                System.out.println("语音连接成功！");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {  //打开语音
                outAudio = socketAudio.getOutputStream();
                inAudio = socketAudio.getInputStream();
                targetDataLine = AudioUtils.getTargetDataLine();
                sourceDataLine = AudioUtils.getSourceDataLine();
                System.out.println("server:");
                new Thread(new SendAudio(bos,targetDataLine,outAudio)).start();
                new Thread(new ReceiveAudio(bis,inAudio,sourceDataLine)).start();
            } catch (Exception e2) {
                return;
            }
        }
        else if(e.getActionCommand().equals("关闭语音")){
            System.out.println("关闭语音");
            jButton.setText("开启语音");
            try {
                socketAudio.shutdownInput();
                socketAudio.shutdownOutput();
                socketAudio.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
    @Override
    public void keyTyped(KeyEvent e) { }
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_ENTER){ //回车键
            senddata();
        }
    }
    @Override
    public void keyReleased(KeyEvent e) { }
    public void sendFile() { //发送文件
        try {
            File file = new File(fullPath);
            FileInputStream fileInputStream = new FileInputStream(file);
            DataInputStream dis=new DataInputStream(fileInputStream);
            OutputStream outputStream = socketFile.getOutputStream();
            DataOutputStream dos=new DataOutputStream(outputStream);
            dos.writeLong(file.length());
            String FileInfo=input.length()+"@"+input+"@"+file.getName(); //传输信息如：2@张三@文件名
            dos.writeUTF(FileInfo);
            System.out.println("长度："+file.length());
            String text="《"+file.getName()+"》已经发送！";
            String name=input;
            String nameL=name.substring(name.length()-2);
            JLabel msglabel=new JLabel(text+System.lineSeparator()); //设置在自己的界面显示
            msglabel.setFont(new Font("微软雅黑",Font.BOLD,18));
            msglabel.setForeground(Color.WHITE);
            msglabel.setBackground(new Color(1,153,255));
            msglabel.setSize(830,30);
            msglabel.setOpaque(true);
            FlowLayout flowLayout = new FlowLayout(); //装消息面板（每一行一个）
            JPanel itempanel=new JPanel(flowLayout);
            FlowLayout layout = (FlowLayout) itempanel.getLayout();
            layout.setAlignment(FlowLayout.RIGHT);
            JButton jButton=new RoundButton(nameL);
            Insets insets = new Insets(-5, -5, -5, -5);
            jButton.setMargin(insets);
            Font font = new Font("微软雅黑", Font.BOLD, 18);
            jButton.setFont(font);
            Dimension dimension = new Dimension(48, 48);
            jButton.setPreferredSize(dimension);
            jButton.setBackground(Color.BLUE);
            jButton.setForeground(Color.WHITE);
            GridLayout gridLayout = new GridLayout(2, 1);
            JPanel panelL=new JPanel(gridLayout);
            JLabel label=new JLabel(name);
            label.setHorizontalAlignment(SwingConstants.RIGHT);
            panelL.add(label);
            panelL.add(msglabel);
            Color color = new Color(241, 241, 241);
            itempanel.setBackground(color);
            itempanel.setBounds(0,a,830,55);
            a+=60;
            itempanel.add(panelL);
            itempanel.add(jButton);
            panel4.add(itempanel);
            panel4.updateUI();
            Dimension dimension1 = new Dimension(830, a);
            panel4.setPreferredSize(dimension1);
            int count=-1,sum=0;
            byte[] buffer=new byte[1024*1024];
            while ((count=dis.read(buffer))!=-1){
                dos.write(buffer,0,count);
                sum+=count;
                System.out.println("以传输"+sum+"byte");
            }
            System.out.println("发送完毕");
            dos.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void senddata(){ //发送文字
        String name=input;
        String nameL=name.substring(name.length()-2);
        String text = jtf.getText(); //获得文本框内容
        String sendtext=name.length()+name+jtf.getText(); //发送信息名字长度+名字+文本
        JLabel msglabel=new JLabel(text+System.lineSeparator());  //设置在自己的界面显示
        msglabel.setFont(new Font("微软雅黑",Font.BOLD,18));
        msglabel.setForeground(Color.WHITE);
        msglabel.setBackground(new Color(1,153,255));
        msglabel.setSize(830,30);
        msglabel.setOpaque(true);
        FlowLayout flowLayout = new FlowLayout(); //装消息面板（每一行一个）
        JPanel itempanel=new JPanel(flowLayout);
        FlowLayout layout = (FlowLayout) itempanel.getLayout();
        layout.setAlignment(FlowLayout.RIGHT);
        JButton jButton=new RoundButton(nameL);
        Insets insets = new Insets(-5, -5, -5, -5);
        jButton.setMargin(insets);
        Font font = new Font("微软雅黑", Font.BOLD, 18);
        jButton.setFont(font);
        Dimension dimension = new Dimension(48, 48);
        jButton.setPreferredSize(dimension);
        jButton.setBackground(Color.BLUE);
        jButton.setForeground(Color.WHITE);
        GridLayout gridLayout = new GridLayout(2, 1);
        JPanel panelL=new JPanel(gridLayout);
        Color color = new Color(241, 241, 241);
        panelL.setBackground(color);
        JLabel label=new JLabel(name);
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        panelL.add(label);
        panelL.add(msglabel);
        Color color1 = new Color(241, 241, 241);
        itempanel.setBackground(color1);
        itempanel.setBounds(0,a,830,55);
        a+=60;
        itempanel.add(panelL);
        itempanel.add(jButton);
        panel4.add(itempanel);
        panel4.updateUI();
        Dimension dimension1 = new Dimension(830, a);
        panel4.setPreferredSize(dimension1);
        try {
            OutputStream outputStream = socket.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
            printWriter = new PrintWriter(outputStreamWriter);
            System.out.println("给服务端发送消息！"+sendtext);
            printWriter.println(sendtext);
            printWriter.flush();
            //清空
            jtf.setText("");
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }finally {
        }
    }
    class receiveFile implements Runnable{ //接受文件
        String savePath;
        Socket socketFile;
        String localPath;
        public receiveFile(String savePath,Socket socketFile) {
            this.savePath = savePath;
            this.socketFile=socketFile;
            localPath=savePath;
        }
        @Override
        public void run() {
            while (true){
                System.out.println("Thread running>>>");
                DataInputStream dataInputStream=null;
                try {
                    InputStream inputStream = socketFile.getInputStream();
                    dataInputStream=new DataInputStream(inputStream);
                    int bufferSize = 1024;
                    byte[] buf = new byte[bufferSize*bufferSize];
                    int passedlen = 0;
                    long len = 0;
                    String readUTF = dataInputStream.readUTF(); //服务端传送来的文件信息
                    System.out.println("服务端："+readUTF);
                    if(readUTF.equals("")){
                        continue;
                    }
                    String[] split = readUTF.split("@");
                    for(String s:split){
                        System.out.println(s);
                    }
                    String fileName = split[1];
                    String s1 = split[0];
                    int anInt = Integer.parseInt(String.valueOf(s1.charAt(0))); //服务端姓名长度
                    String names = s1.substring(1, anInt + 1); //服务端姓名
                    System.out.println("hello2");
                    if(names.equals(input)||names.equals("所有人")){
                        String text="接收文件《"+fileName+"》，自动保存在D:\\BigProgram目录下！"; //显示文件消息
                        String servername=names;
                        String nameL=servername.substring(servername.length()-2);
                        JLabel msglabel=new JLabel(text+System.lineSeparator());
                        msglabel.setFont(new Font("微软雅黑",Font.BOLD,18));
                        msglabel.setForeground(Color.BLACK);
                        msglabel.setBackground(new Color(241,241,241));
                        msglabel.setSize(840,30);
                        msglabel.setOpaque(true);
                        FlowLayout flowLayout = new FlowLayout(); //消息面板
                        JPanel itempanel=new JPanel(flowLayout);
                        FlowLayout layout=(FlowLayout)itempanel.getLayout();
                        layout.setAlignment(FlowLayout.LEFT);
                        JButton jButton1=new RoundButton(nameL);
                        Insets insets = new Insets(-5, -5, -5, -5);
                        jButton1.setMargin(insets);
                        Font font = new Font("微软雅黑", Font.BOLD, 18);
                        jButton1.setFont(font);
                        Dimension dimension = new Dimension(48, 48);
                        jButton1.setPreferredSize(dimension);
                        jButton1.setBackground(Color.BLUE);
                        jButton1.setForeground(Color.WHITE);
                        GridLayout gridLayout = new GridLayout(2, 1);
                        JPanel jPanel1=new JPanel(gridLayout);
                        JLabel label1=new JLabel(servername);
                        jPanel1.add(label1);
                        jPanel1.add(msglabel);
                        Color color = new Color(241, 241, 241);
                        itempanel.setBackground(color);
                        itempanel.setBounds(0,a,840,55);
                        a+=60;
                        itempanel.add(jButton1);
                        itempanel.add(jPanel1);
                        panel4.add(itempanel);
                        panel4.updateUI();
                        Dimension dimension1 = new Dimension(840, a);
                        panel4.setPreferredSize(dimension1);
                        System.out.println("hello");
                        savePath+=(int)(Math.random()*100);
                        savePath+=fileName;
                        File file = new File(savePath);
                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        DataOutputStream fileOut=new DataOutputStream(fileOutputStream);
                        len=dataInputStream.readLong();
                        System.out.println("文件的长度为:" + len + "    KB");
                        System.out.println("开始接收文件!");
                        int count=-1,sum=0;
                        while ((count=dataInputStream.read(buf))!=-1){
                            fileOut.write(buf,0,count);
                            sum+=count;
                            System.out.println("已结收" + sum + "比特");
                            if(sum==len)
                                break;
                        }
                        fileOut.flush();
                        this.savePath=localPath;
                    }
                    if(!names.equals(input)){
                        continue;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    class receive implements Runnable{ //接受文字信息
        Socket socket1;
        public receive(Socket socket) {
            this.socket1 = socket;
        }
        @Override
        public void run() {
            BufferedReader bufferedReader=null;
            try {
                InputStream inputStream = socket.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                bufferedReader=new BufferedReader(inputStreamReader);
                while (true){
                    String readLine = bufferedReader.readLine();
                    System.out.println("服务器的消息为：" + readLine);
                    System.out.println(readLine); //将读取数据拼接到文本域显示
                    String receiveText=readLine; //收到信息
                    Integer clientCnt = Integer.parseInt(String.valueOf(receiveText.charAt(1))); //客户端姓名长度
                    System.out.println(clientCnt);
                    String clentname=receiveText.substring(2,2+clientCnt);  //客户端姓名
                    System.out.println(clentname);
                    int serverCnt = Integer.parseInt(String.valueOf(receiveText.charAt(receiveText.indexOf('#') + 1))); //服务端姓名长度
                    System.out.println(serverCnt);
                    String servername=receiveText.substring(receiveText.indexOf('#')+2,receiveText.indexOf('#')+2+serverCnt);//服务端姓名
                    System.out.println(servername);
                    String text=receiveText.substring(receiveText.indexOf('#')+2+serverCnt);
                    System.out.println(text);
                    if(clentname.equals(input)||clentname.equals("所有人")){
                        String nameL=servername.substring(servername.length()-2);
                        JLabel msglabel=new JLabel(text+System.lineSeparator());
                        Font font = new Font("微软雅黑", Font.BOLD, 18);
                        msglabel.setFont(font);
                        msglabel.setForeground(Color.BLACK);
                        Color color = new Color(241, 241, 241);
                        msglabel.setBackground(color);
                        msglabel.setSize(830,30);
                        msglabel.setOpaque(true);
                        FlowLayout flowLayout = new FlowLayout(); //消息面板
                        JPanel itempanel=new JPanel(flowLayout);
                        FlowLayout layout=(FlowLayout)itempanel.getLayout();
                        layout.setAlignment(FlowLayout.LEFT);
                        JButton jButton1=new RoundButton(nameL);
                        Insets insets = new Insets(-5, -5, -5, -5);
                        jButton1.setMargin(insets);
                        Font font1 = new Font("微软雅黑", Font.BOLD, 18);
                        jButton1.setFont(font1);
                        Dimension dimension = new Dimension(48, 48);
                        jButton1.setPreferredSize(dimension);
                        jButton1.setBackground(Color.BLUE);
                        jButton1.setForeground(Color.WHITE);
                        GridLayout gridLayout = new GridLayout(2, 1);
                        JPanel jPanel1=new JPanel(gridLayout);
                        JLabel label1=new JLabel(servername);
                        jPanel1.add(label1);
                        jPanel1.add(msglabel);
                        Color color1 = new Color(241, 241, 241);
                        itempanel.setBackground(color1);
                        itempanel.setBounds(0,a,830,55);
                        a+=60;
                        itempanel.add(jButton1);
                        itempanel.add(jPanel1);
                        panel4.add(itempanel);
                        panel4.updateUI();
                        Dimension dimension1 = new Dimension(830, a);
                        panel4.setPreferredSize(dimension1);
                     }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
            }
        }
    }
}
class ClientWindow extends JFrame implements Serializable { //客户端窗体
    private ObjectOutputStream oos;
    private OutputStream socketOutputStream;
    private InputStream socketInputStream;
    private JLabel label;
    private String ip;
    public void repainImage(byte[] imageBytes) { // 重写背景图片方法
        ImageIcon imageIcon = new ImageIcon(imageBytes);
        label.setIcon(imageIcon);
        this.repaint();
    }
    public ClientWindow(OutputStream ost, InputStream ist,String ip) throws IOException {
        this.socketOutputStream = ost;
        this.socketInputStream = ist;
        this.ip=ip;
        this.oos = new ObjectOutputStream(ost);
        this.setTitle("客户端"+ip+"的画面");
        label = new JLabel();
        JPanel bp = new JPanel();
        JPanel p = new JPanel();
        BorderLayout borderLayout = new BorderLayout();
        this.setLayout(borderLayout);
        p.add(label);
        JScrollPane scroll = new JScrollPane(p);// 给p面板添加滚动条
        this.add(bp, BorderLayout.NORTH);
        this.add(scroll, BorderLayout.CENTER);
        this.setSize(1024, 768);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setVisible(true);
        this.setLabelListener();//设置面板各种事件
    }
    public void setLabelListener() { //设置面板各种事件
        label.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                System.out.println(2);
            }
            @Override
            public void keyReleased(KeyEvent e) {
                KeyInfo info = new KeyInfo(e.getID(), e.getKeyCode());
                System.out.println(1);
                sendEvent(info);
            }
            @Override
            public void keyPressed(KeyEvent e) {
                KeyInfo info = new KeyInfo(e.getID(), e.getKeyCode());
                sendEvent(info);
            }
        });
        label.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseMoved(MouseEvent e) {
                sendEvent(e);
            }
            @Override
            public void mouseDragged(MouseEvent e) {
                sendEvent(e);
            }
        });
        label.addMouseWheelListener(new MouseWheelListener() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                sendEvent(e);
            }
        });
        label.addMouseListener(new MouseListener() {
            @Override
            public void mouseReleased(MouseEvent e) {
                label.requestFocus();
                sendEvent(e);
            }
            @Override
            public void mousePressed(MouseEvent e) {
                sendEvent(e);
            }
            @Override
            public void mouseClicked(MouseEvent e) {
                sendEvent(e);
            }
            @Override
            public void mouseEntered(MouseEvent e) {
            }
            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
    }
    public void sendEvent(InputEvent event) {
        try {
            oos.writeObject(event);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
    public void sendEvent(KeyInfo event) {
        synchronized (oos){
            try {
                oos.writeObject(event);
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(-1);
            }
        }
    }
}

